package chant.config;

import chant.controller.IndexController;
import chant.model.Area;
import chant.model.Book;
import chant.model.Chant;
import chant.util.AreaUtil;
import chant.util.BookUtil;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

/**
 * Create-Date: 2016/6/16
 * Description:
 */
public class MainConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants me) {
        loadPropertyFile("config.properties");

        me.setMainRenderFactory(new BeetlRenderFactory());

        GroupTemplate gt = BeetlRenderFactory.groupTemplate;
        // 可以通过gt设置全局共享变量

        me.setDevMode(PropKit.getBoolean("devMode", false));
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", IndexController.class);
    }

    @Override
    public void configPlugin(Plugins me) {
        // 配置C3p0数据库连接池插件
        C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
        me.add(c3p0Plugin);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        me.add(arp);
        arp.addMapping("book", Book.class);
        arp.addMapping("area", Area.class);
        arp.addMapping("chant", Chant.class);
    }

    @Override
    public void configInterceptor(Interceptors me) {

    }

    @Override
    public void configHandler(Handlers me) {
        AreaUtil.loadData();
        BookUtil.loadData();
    }

    public static void main(String[] args) {
        JFinal.start("WebRoot", 8080, "/", 5);
    }
}
